package org.visionlibrary.image.filters.bluring;

import java.awt.Point;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.model.WindowFilter;


public class AlphaTrimmed extends WindowFilter {
	protected int width;

	protected int height;

	protected int trimmer = 6;

	public AlphaTrimmed(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public AlphaTrimmed(int size) {
		this(size, size);
	}

	public AlphaTrimmed() {
		this(3);
	}

	protected int getNewPixelVal(TiledImage src, Point p, int channel) {
		int[] alphatrimv = new int[windowHeight * windowWidth];
		Point pIter = new Point();
		int o = 0;
		for (pIter.y = 0; pIter.y < windowHeight; pIter.y++)
			for (pIter.x = 0; pIter.x < windowWidth; pIter.x++)
				alphatrimv[o++] = getWindowPixelVal(src, pIter, channel);

		java.util.Arrays.sort(alphatrimv);

		o = 0; //reuse for memory purpose
		for (int i = trimmer/2 - 1; i < alphatrimv.length - trimmer/2; i++) 
			o += alphatrimv[i];

		return (o/(alphatrimv.length - trimmer));
	}

	protected void setWindowProperties() {
		windowWidth = width;
		windowHeight = height;
		dstPointInWindow = new Point(width / 2, height / 2);
	}

	public int getTrimmer() {
		return trimmer;
	}

	public void setTrimmer(int trimmer) {
		if (trimmer >= height * width || trimmer <= 0)
			this.trimmer = (height * width) - 2;
		else
			this.trimmer = trimmer;
	}
}
